﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using HGZP.PLL;
using HGZP.Model;


namespace HGZP.DAL
{
    [Serializable]
    public class DBSave
    {
        public List<string> sqlStringList = new List<string>();
        public List<DataSetList> DataSetUpdateList = new List<DataSetList>();
        //
        public void SetUpdateSqlString(string sUpdateSql)
        {
            sqlStringList.Add(sUpdateSql);
        }
        //
        public void SetUpdateDataSet(DataSetList updateDataSet)
        {
            DataSetUpdateList.Add(updateDataSet);
        }
        //
        public void PrepareSave(string sSql)
        {
            SetUpdateSqlString(sSql);
        }
        public void PrepareSave(ICTable myTable)
        {
            DataSetList ds = new DataSetList();
            ds.Ds_Save = myTable.A_DataSet;
            ds.SelectSqlString = myTable.A_SqlString;
            //
            if (myTable.A_DataSet.GetChanges() != null)
            {
                SetUpdateDataSet(ds);
            }
            //
        }
        public void PrepareSave(DataSet myDataSet, string myString)
        {
            DataSetList ds = new DataSetList();
            ds.Ds_Save = myDataSet;
            ds.SelectSqlString = myString;
            //
            if (myDataSet.GetChanges() != null)
            {
                SetUpdateDataSet(ds);
            }
            //
        }
        //
        public void PrepareLogSave(ICTable myTable,  string sRemark)
        {
          /*  CreateWorkOperatingLog myWorkLog = new CreateWorkOperatingLog();
            myWorkLog.MyCreateWorkOperatingLog(myTable.A_DataSet.GetChanges(),  sRemark);
            DataSetList ds = new DataSetList();
            ds.Ds_Save = myWorkLog.operatingLogRowCollection.A_DataSet;
            ds.SelectSqlString = myWorkLog.operatingLogRowCollection.A_SqlString;
            //
            if (myWorkLog.operatingLogRowCollection.A_DataSet != null)
            {
                SetUpdateDataSet(ds);
            }*/
            //
        }
        public void PrepareLogSave(ICTable myTable)
        {
            /*CreateWorkOperatingLog myWorkLog = new CreateWorkOperatingLog();
            myWorkLog.MyCreateWorkOperatingLog(myTable.A_DataSet.GetChanges(),  string.Empty);
            DataSetList ds = new DataSetList();
            ds.Ds_Save = myWorkLog.operatingLogRowCollection.A_DataSet;
            ds.SelectSqlString = myWorkLog.operatingLogRowCollection.A_SqlString;
            //
            if (myWorkLog.operatingLogRowCollection.A_DataSet != null)
            {
                SetUpdateDataSet(ds);
            }*/
            //
        }
        public void PrepareLogSave(DataTable myTable,  string sRemark)
        {
            /*if (myTable == null) { return; }
            CreateWorkOperatingLog myWorkLog = new CreateWorkOperatingLog();
            myWorkLog.MyCreateWorkLog(myTable.GetChanges(), sRemark);
            DataSetList ds = new DataSetList();
            ds.Ds_Save = myWorkLog.operatingLogRowCollection.A_DataSet;
            ds.SelectSqlString = myWorkLog.operatingLogRowCollection.A_SqlString;
            //
            if (myWorkLog.operatingLogRowCollection.A_DataSet != null)
            {
                SetUpdateDataSet(ds);
            }*/
            //
        }
        //
        public bool Save()
        {
            //if (SqlHelp.locationType == LocationType.remote)
            //{
            //    XM.WebServiceXM myweb = new HGZP.BLL.XM.WebServiceXM();
            //    string gsConfigPublic = ConfigurationPath.MyConfigurationPath + @"\connect.config";
            //    OperateXMLFile configPublic = new OperateXMLFile(gsConfigPublic);
            //    myweb.Url = configPublic.GetXmlFileValue("RemoteWebAddress");
            //    //



            //    string[] myString = new string[sqlStringList.Count];
            //    DataSet[] myDataSet = new DataSet[DataSetUpdateList.Count];
            //    string[] myDataSetString = new string[DataSetUpdateList.Count];

            //    myString = sqlStringList.ToArray();
            //    for (int i = 0; i < DataSetUpdateList.Count; i++)
            //    {
            //        DataSetList dsList = new DataSetList();
            //        dsList.SelectSqlString = DataSetUpdateList[i].SelectSqlString;
            //        dsList.Ds_Save = DataSetUpdateList[i].Ds_Save;
            //        myDataSet[i] = dsList.Ds_Save;
            //        myDataSetString[i] = dsList.SelectSqlString;
            //    }


            //    string sReturn = myweb.SetDbSave(myString, myDataSet, myDataSetString);
            //    if (sReturn.Trim() != string.Empty)
            //    {
            //        MyTip.ErrorTip(sReturn);
            //    }
            //    else
            //    {
            //        for (int i = 0; i < DataSetUpdateList.Count; i++)
            //        {
            //            DataSetUpdateList[i].Ds_Save.AcceptChanges();
            //        }
            //    }
            //    //
            //    return true;
            //}
            //
            SqlHelp mySqlHelp = new SqlHelp();
            try
            {
                mySqlHelp.TransactionBegin();
                int i = 0;
                //更新Sql语法
                if (sqlStringList.Count != 0)
                {
                    string[] sqlStringMulti = new string[sqlStringList.Count];
                    i = 0;
                    foreach (string mySql in sqlStringList)
                    {
                        if (sqlStringList[i].Trim() != string.Empty)
                        {
                            sqlStringMulti[i] = sqlStringList[i];
                            i++;
                        }
                    }
                    mySqlHelp.UpdateDataBySqlMulti(sqlStringMulti);
                }
                //更新DataSet
                DataSet[] MultiDataSet = new DataSet[DataSetUpdateList.Count];
                string[] MultiSqlstring = new string[DataSetUpdateList.Count];
                i = 0;
                foreach (DataSetList mytable in DataSetUpdateList)
                {
                    if (mytable.Ds_Save.GetChanges() != null)
                    {
                        MultiDataSet[i] = mytable.Ds_Save.GetChanges();
                        MultiSqlstring[i] = mytable.SelectSqlString;
                        i++;
                    }
                }
                //
                if (i > 0)
                {
                    mySqlHelp.UpdateByDataSetMulti(MultiDataSet, MultiSqlstring);

                }
                //
                mySqlHelp.TransactionEnd();
                //
                foreach (DataSetList mytable in DataSetUpdateList)
                {
                    mytable.Ds_Save.AcceptChanges();
                    sqlStringList.Clear();
                }
                return true;
            }
            catch (Exception e)
            {
                try
                {
                    mySqlHelp.myConnection.Close();
                }
                catch
                {
                }
                Public.globalErrorString = e.Message.ToString();
                return false;
            }
        }
        //
    }
    [Serializable]
    public class DataSetList
    {
        private DataSet ds_Save = new DataSet();
        private string selectSqlString = string.Empty;
        //
        public DataSet Ds_Save
        {
            get { return ds_Save; }
            set { ds_Save = value; }
        }
        public string SelectSqlString
        {
            get { return selectSqlString; }
            set { selectSqlString = value; }
        }
        //

        //
    }
}
